home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgramD2.iso / Borland / Borland C++ V5.02 / OWLSRC.PAK / RESOURCE.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1997-05-06  |  7.5 KB  |  329 lines

  1. //----------------------------------------------------------------------------
  2. // ObjectWindows
  3. // Copyright (c) 1991, 1997 by Borland International, All Rights Reserved
  4. //
  5. //$Revision:   10.6  $
  6. //
  7. // Implementation classes handling Windows resources
  8. //----------------------------------------------------------------------------
  9. #include <owl/pch.h>
  10. #if !defined(OWL_RESOURCE_H) 
  11. # include <owl/resource.h>
  12. #endif
  13. #if !defined(OWL_GDIOBJEC_H)
  14. # include <owl/gdiobjec.h>
  15. #endif
  16. #if !defined(OWL_GADGETWI_H)
  17. # include <owl/gadgetwi.h>
  18. #endif
  19. #if !defined(OWL_BUTTONGA_H)
  20. # include <owl/buttonga.h>
  21. #endif
  22. #if !defined(OWL_CELARRAY_H)
  23. # include <owl/celarray.h>
  24. #endif
  25.  
  26. OWL_DIAGINFO;
  27.  
  28. //
  29. //
  30. //
  31. TDialogRes::TDialogRes(HINSTANCE hInst, TResId id) 
  32.            : OldResource(0)
  33. #if defined(BI_PLAT_WIN32)
  34.            , NewResource(0)
  35. #endif
  36. {
  37. #if defined(BI_PLAT_WIN32)
  38.   // In 32-bit, favour the new dialog resource over the old one
  39.   //
  40.   NewResource = new TResource<DLGTEMPLATEEX, RT_NEWDIALOG>(hInst, id);
  41.   if (!NewResource->IsOK()) 
  42. #endif
  43.     // In 16-bit (or when the new resource cannot be found in 32-bit), try
  44.     // the old DLGTEMPLATE resource
  45.     //
  46.     OldResource = new TResource<DLGTEMPLATE, RT_DIALOG>(hInst, id);
  47. }
  48.  
  49. //
  50. //
  51. //
  52. #if defined(BI_PLAT_WIN32)
  53. TDialogRes::TDialogRes(HINSTANCE hInst, TResId id, LANGID langid)
  54.            :OldResource(0)
  55. #if defined(BI_PLAT_WIN32)
  56.           , NewResource(0)
  57. #endif
  58. {
  59.   NewResource = new TResource<DLGTEMPLATEEX, RT_NEWDIALOG>(hInst, id, langid);
  60.   if (!NewResource->IsOK())
  61.     OldResource = new TResource<DLGTEMPLATE, RT_DIALOG>(hInst, id, langid);
  62. }
  63. #endif
  64.  
  65. //
  66. //
  67. //
  68. TDialogRes::~TDialogRes()
  69. {
  70.   delete OldResource;
  71. #if defined(BI_PLAT_WIN32)
  72.   delete NewResource;
  73. #endif
  74. }
  75.  
  76.  
  77. //
  78. //
  79. //
  80. bool
  81. TDialogRes::IsOK() const
  82. {
  83. #if   defined(BI_PLAT_WIN16)
  84.   return OldResource && OldResource->IsOK();
  85. #elif defined(BI_PLAT_WIN32)
  86.   return (NewResource && NewResource->IsOK()) || 
  87.          (OldResource && OldResource->IsOK());
  88. #endif
  89. }
  90.  
  91.  
  92. //
  93. //
  94. //
  95. #if defined(BI_PLAT_WIN32)
  96. bool      
  97. TDialogRes::IsDialogEx() const 
  98. {
  99.   PRECONDITION(IsOK());
  100.   return (!OldResource || !OldResource->IsOK()) && 
  101.          ( NewResource &&  NewResource->IsOK());
  102. }
  103.  
  104. DLGTEMPLATEEX*
  105. TDialogRes::GetTemplateEx() const
  106. {
  107.   PRECONDITION(IsDialogEx());
  108.   return NewResource->operator DLGTEMPLATEEX*();
  109. }
  110. #endif
  111.  
  112. DLGTEMPLATE*
  113. TDialogRes::GetTemplate() const
  114. {
  115. #if defined(BI_PLAT_WIN32)
  116.   PRECONDITION(!IsDialogEx());
  117. #endif
  118.   return  OldResource->operator DLGTEMPLATE*();
  119. }
  120.  
  121. void      
  122. TDialogRes::GetRect(TRect& rect) const
  123. {
  124. #if defined(BI_PLAT_WIN32)
  125.   if (!IsDialogEx()) {
  126. #endif
  127.     DLGTEMPLATE* dlgTemplate = GetTemplate();
  128.     rect.left  = dlgTemplate->x;
  129.     rect.right = dlgTemplate->x + dlgTemplate->cx;
  130.     rect.top   = dlgTemplate->y;
  131.     rect.bottom= dlgTemplate->y + dlgTemplate->cy;
  132. #if defined(BI_PLAT_WIN32)
  133.   } else {
  134.     DLGTEMPLATEEX* dlgTemplate = GetTemplateEx();
  135.     rect.left  = dlgTemplate->x;
  136.     rect.right = dlgTemplate->x + dlgTemplate->cx;
  137.     rect.top   = dlgTemplate->y;
  138.     rect.bottom= dlgTemplate->y + dlgTemplate->cy;
  139.   }
  140. #endif 
  141. }
  142.  
  143. int
  144. TDialogRes::GetText(char far* buffer, int size, TDlgResText which) const
  145. {
  146. #if   defined(BI_PLAT_WIN16)
  147.   DLGTEMPLATE* dlgTemplate = GetTemplate();
  148.   char far* p = (char far*)(dlgTemplate+1);
  149.   char far* pMenu  = p;
  150.   char far* pClass = pMenu + lstrlen(pMenu) + 1;
  151.   char far* pCaption = pClass + lstrlen(pMenu) + 1;
  152.  
  153.   switch (which) {
  154.     case drtMenuName:   p = pMenu;    break;
  155.     case drtClassName:  p = pClass;   break;
  156.     case drtCaption:    p = pCaption; break;
  157.     default:
  158.       return 0;
  159.   };
  160.   p = lstrcpyn(buffer, p, size);
  161.   return p ? lstrlen(p) : 0;
  162.   
  163. #elif defined(BI_PLAT_WIN32)
  164.   
  165.   LPCWSTR p, pMenu, pClass, pCaption;
  166.  
  167.   if (!IsDialogEx()) {
  168.     DLGTEMPLATE* dlgTemplate = GetTemplate();
  169.     p = (LPCWSTR)(dlgTemplate+1);
  170.     pMenu = p;
  171.     pClass = pMenu + ((*pMenu == 0xffff) ? 2 : lstrlenW(pMenu)+1);
  172.     pCaption = pClass + ((*pClass == 0xffff) ? 2 : lstrlenW(pClass)+1);
  173.   } else {
  174. //    DLGTEMPLATEEX* dlgTemplateEx = GetTemplateEx();
  175.   }
  176.  
  177.   switch (which) {
  178.     case drtMenuName:   p = pMenu;    break;
  179.     case drtClassName:  p = pClass;   break;
  180.     case drtCaption:    p = pCaption; break;
  181.     default:
  182.       return 0;
  183.   };
  184.  
  185.   return WideCharToMultiByte(CP_ACP, 0, p, (*p == 0xfff) ? 2 : lstrlenW(p), 
  186.                              buffer, size, 0, 0);
  187. #endif 
  188. }
  189.  
  190. #if defined(BI_PLAT_WIN32)
  191. //
  192. //
  193. //
  194. TToolbarRes::TToolbarRes(HINSTANCE hInst, TResId id, TAutoDelete autoDelete)
  195.             :TResource<TOOLBARTEMPLATE, RT_TOOLBAR>(hInst, id), ToolbarBitmap(0)
  196. {
  197.   ShouldDelete  = (autoDelete == AutoDelete);
  198.  
  199.   // Following will throw exception in case of failure
  200.   //
  201.   ToolbarBitmap = new TBitmap(hInst, id);
  202. }
  203.  
  204. //
  205. //
  206. //
  207. TToolbarRes::~TToolbarRes()
  208. {
  209.   if (ShouldDelete)
  210.     delete ToolbarBitmap;
  211. }
  212.  
  213. //
  214. //
  215. //
  216. TOOLBARTEMPLATE&
  217. TToolbarRes::TBInfo() const {
  218.   PRECONDITION(IsOK());
  219.   return *(CONST_CAST(TToolbarRes*, this)->operator TOOLBARTEMPLATE*());
  220. }
  221.  
  222. //
  223. //
  224. //
  225. TBitmap&
  226. TToolbarRes::GetBitmap() {
  227.   PRECONDITION(IsOK());
  228.   PRECONDITION(ToolbarBitmap && ToolbarBitmap->GetHandle());
  229.   return *ToolbarBitmap;
  230. }
  231.  
  232. //
  233. //
  234. //
  235. int
  236. TToolbarRes::GetCount() const {
  237.   PRECONDITION(IsOK());
  238.   return TBInfo().count;    
  239. }
  240.  
  241. //
  242. //
  243. //
  244. ushort*
  245. TToolbarRes::GetIds() const {
  246.   PRECONDITION(IsOK());
  247.   return TBInfo().ids;
  248. }
  249.  
  250. //
  251. //
  252. //
  253. int
  254. TToolbarRes::GetWidth() const {
  255.   PRECONDITION(IsOK());
  256.   return TBInfo().width;
  257. }
  258.  
  259. //
  260. //
  261. //
  262. int
  263. TToolbarRes::GetHeight() const {
  264.   PRECONDITION(IsOK());
  265.   return TBInfo().height;
  266. }
  267.  
  268.  
  269. //
  270. //
  271. TToolbarBldr::TToolbarBldr(TGadgetWindow& win, TResId id, HINSTANCE hinst)
  272.              :Win(win), TbarRes(0), Id(id), Hinst(hinst)
  273. }
  274.  
  275. //
  276. //
  277. //
  278. TToolbarBldr::~TToolbarBldr()
  279. {
  280.   delete TbarRes;
  281. }
  282.  
  283. //
  284. // Populate the gadgetwindow with button gadgets and separators using a
  285. // bitmap and toolbar resource pair. Returns true if the resource was
  286. // successfully loaded and the gadgets created and false otherwise.
  287. //
  288. void
  289. TToolbarBldr::Build()
  290. {
  291.   // Default to window's module if none was specified
  292.   //
  293.   if (!Hinst)
  294.     Hinst = *(Win.GetModule());
  295.  
  296.   // NOTE: Don't let the TToolbarRes instance own the bitmap, we'll hand
  297.   //       it to the celarray instead.
  298.   //
  299.   TbarRes = new TToolbarRes(Hinst, Id, NoAutoDelete);
  300.  
  301.   CHECK(TbarRes->IsOK());
  302.   TCelArray* celArray = new TCelArray(&TbarRes->GetBitmap(), 
  303.                                       TbarRes->GetCount(),
  304.                                       TSize(TbarRes->GetWidth(), 
  305.                                             TbarRes->GetHeight()));
  306.   // Hand celarray to gadgetwindow
  307.   // NOTE: Nice enhancement would be to add/merge toolbar resources using
  308.   //       TCelArray's Add(TCelArray& src, index) method if the window
  309.   //       already has a celarray. Unfortunately, the tbar resource does 
  310.   //       provide a method to specify insertion indices to make this
  311.   //       approach usable. So we'll wipe out the previous celarray.
  312.   //
  313.   Win.SetCelArray(celArray);
  314.  
  315.   // Create gadgets based on resource information
  316.   //
  317.   for (int i=0, j=0; i<TbarRes->GetCount(); i++) {
  318.     if (TbarRes->GetIds()[i] == 0)
  319.       Win.Insert(*new TSeparatorGadget(6));
  320.     else {
  321.       Win.Insert(*new TButtonGadget(j++, TbarRes->GetIds()[i], 
  322.                                     TButtonGadget::Command, false, 
  323.                                     TButtonGadget::Up, true));
  324.     }
  325.   }
  326. }
  327. #endif  //  BI_PLAT_WIN32
  328.